<?php

/**
 * MVM_MALL 网上商店系统  推广记录管理
 * ============================================================================
 * 版权所有 (C) 2007-2010 www.mvmmall.com，并保留所有权利。
 * 网站地址: http://www.mvmmall.com
 * ----------------------------------------------------------------------------
 * 这是一个免费开源的软件；这意味着您可以在不用于商业目的的前提下对程序代码
 * 进行修改、使用和再发布。
 * ============================================================================
 * $Author:  www.mvmmall.com  $
 * $Date: 2008-09-27 $
 * $Id: promotion.inc.php www.mvmmall.com$
 * ---------------------------------------------
*/

if(!defined('MVMMALL') || !defined('IN_ADMINCP')) exit('Access Denied');
if($action=='list'){
    $pro_rt      = array();
    $scarch_sql  = "where point_category = '6'";
    $ps_member   && $scarch_sql .= " AND modify_id = '".filtering($ps_member)."'";
    require_once MVMMALL_ROOT.'./include/pager.class.php';
    $total_count = $db->counter("{$tablepre}point_table",$scarch_sql);
    $page        = $page ? (int)$page:1;
    $list_num    = 15;
    $rowset      = new Pager($total_count,$list_num,$page);
    $from_record = $rowset->_offset();
    $result = $db->query("SELECT * FROM `{$tablepre}point_table` $scarch_sql ORDER BY `uid` DESC LIMIT $from_record,$list_num");
    while($rt = $db->fetch_array($result)) {
        $rt['register_date'] = date($mm_date_format,$rt['register_date']);
        $rt['approval']      = $rt['approval']>10 ? "[审定]" : "[未审定]";
        $rt['point_id']      = $rt['point_id'] ? $rt['point_id']:'***';
        $rt['murl']          = "admincp.php?module=$module&action=list&ps_member=$rt[modify_id]";
        $total              += $rt['point_add'];
        $pro_rt[]            = $rt;

    }
    $page_list = $rowset->link("admincp.php?module=$module&action=list&ps_member=$ps_member&page=");
    include template('promotion');
    footer();
}elseif ($action=='add'){
    if ($setp==1&&$_POST){
        //图片
        if($_FILES['gallery']['name'][0]!=''){
            require_once MVMMALL_ROOT.'./include/upfile.class.php';
            $f = new upfile('jpg','images/banner/');
            foreach ($_FILES['gallery']['name'] AS $key => $value){
                $upload = array(
                'name' => $_FILES['gallery']['name'][$key],
                'type' => $_FILES['gallery']['type'][$key],
                'tmp_name' => $_FILES['gallery']['tmp_name'][$key],
                'error' => $_FILES['gallery']['error'][$key],
                'size' => $_FILES['gallery']['size'][$key],
                );
                $img = $f->upload($upload);
                $title = dhtmlchars($title[$key]);
                $link = dhtmlchars($link[$key]);
                $rows = array(
                'title'=> $title,
                'link' =>$link,
                'type_img' =>'1',
                'img'=> $img
                );
                $db->insert("{$tablepre}cycle",$rows);
            }
        }
        $cache->put_cache('info');
        admin_msg('success',dreferer());
    }else {
        $result     = $db->query("SELECT * FROM `{$tablepre}cycle` WHERE type_img='1'");
        while ($rt  = $db->fetch_array($result)){
            $rt['del'] = "admincp.php?module=$module&action=del&uid=$rt[uid]";
            $photo[]   = $rt;
        }
        require_once template('promotion_add');
    }
}
//修改标题和连接
elseif ($action=='edit' && is_numeric($uid)){
    if (isset($title)){
        $title  = iconv('gb2312', 'UTF-8',$title);
        $rows   = array('title'=>$title);
    }else {
        $rows   = array('link'=>$link);
    }
    $rows = dhtmlchars($rows);
    $db->update("{$tablepre}cycle",$rows,$where="uid='$uid'");
    $cache->put_cache('info');
}

//删除图片
elseif ($action=='del' && is_numeric($uid)){
    $rt = $db->get_one("SELECT img,uid FROM `{$tablepre}cycle` WHERE type_img='1' AND uid='$uid'");
    file_unlink($rt['img']);
    $db->query("DELETE FROM `{$tablepre}cycle` WHERE type_img='1' AND uid='$uid'");
    $cache->put_cache('info');
}